package com.cicigodd.brick.plug.upload.controller;

import com.cicigodd.brick.plug.annotation.resource.GetResource;
import com.cicigodd.brick.plug.annotation.resource.PostResource;
import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;
import com.cicigodd.brick.plug.upload.request.SysFileDownRequest;
import com.cicigodd.brick.plug.upload.request.SysFileIdDownRequest;
import com.cicigodd.brick.plug.upload.request.SysFileRequest;
import com.cicigodd.brick.plug.upload.response.SysFileResponse;
import com.cicigodd.brick.plug.upload.service.SysFileService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


@Api(tags = "文件管理")
@RestController
public class SysFileController {

    @Resource
    private SysFileService sysFileService;


    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostResource(name = "文件上传", path = "/sysFile/upload")
    public Result<SysFileResponse> upload(@RequestPart("file") MultipartFile file) {
        SysFileResponse sysFileResponse = sysFileService.upload(file);
        return ResultGenerator.successResult(sysFileResponse);
    }

    /**
     * 通用文件预览，通过传bucket名称和object名称
     *
     * @author cicigodd
     * @date 2020/11/29 11:29
     */
    @ApiOperation(value = "文件预览", notes = "文件预览")
    @GetResource(name = "文件预览", path = "/sysFile/previewByObjectName",requiredPermission = false)
    public void previewByBucketNameObjectName(@Validated SysFileRequest sysFileRequest, HttpServletResponse response) {
        sysFileService.previewByBucketAndObjName(sysFileRequest, response);
    }

    /**
     * 公有文件下载
     *
     * @author cicigodd
     * @date 2020/12/27 13:17
     */
    @ApiOperation(value = "根据文件id下载", notes = "根据文件id下载")
    @GetResource(name = "根据文件id下载", path = "/sysFile/downloadById")
    public void downloadById(@Validated SysFileIdDownRequest sysFileIdDownRequest, HttpServletResponse response) {
        sysFileService.downloadById(sysFileIdDownRequest, response);
    }
    /**
     * 公有文件下载
     *
     * @author cicigodd
     * @date 2020/12/27 13:17
     */
    @ApiOperation(value = "文件下载", notes = "文件下载")
    @GetResource(name = "文件下载", path = "/sysFile/download")
    public void download(@Validated SysFileDownRequest sysFileDownRequest, HttpServletResponse response) {
        sysFileService.download(sysFileDownRequest, response);
    }
}
